Error: invalid operands of types `double' and `double' to binary `operator%'

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jktruimp1
    New Member
    • May 2015
    • 1

    Error: invalid operands of types `double' and `double' to binary `operator%'

    I am trying to reduce fractions.
    If I replace double by int,there is no error,and it will run answers. But the wrong one.....
    I don't know if I use the reducing fractions' rule wrongly.
    Can someone help me pleaseQQ
    Code:
    RationalNumber::RationalNumber(double a,double b)
    {
        if(b>0)//prvent deno<0
        {
            double r;
            if(a<0)//if nume<0
            {
                a=-a;
                r=(a%b);
                while(r!=0)
                {
                    a=b;
                    b=r;
                    r=a%b;
                }
                this->nume=-a;
                this->deno=b;
                cout<<"test2"<<nume<<"/"<<deno<<endl;
            }
            else
            {
                r=a%b;
                while(r!=0)
                {
                    a=b;
                    b=r;
                    r=a%b;
                }
    
                this->nume=a;
                this->deno=b;
                cout<<"test2"<<nume<<"/"<<deno<<endl;
            }
        }
        else
        {
            cout<<"It's a invalid fraction."<<endl;
            cout<<"The fraction will be set to 0/1"<<endl;
            this->nume=0;
            this->deno=1;
            cout<<"test2"<<nume<<"/"<<deno<<endl;
        }
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    The modulus operator % applies only to integers.

    In set theory, the modulo 5 set contains only 0,1,2,3,4. So if you have an element of another set, say the base 10 integer number system, like 17, you convert to modulo five by 17 % 5 which produces 2. That 2 is the element value in the modulo 5 set.

    Floating point, on the other hand, has no conversion to modulo 5 because the values of floating point are not defined. That is, 123.456..... has decimal places that go on forever. It can't be pinned down to a fixed value. Hence, no modulus conversion.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      From a mathematics point of view, a rational number is defined as the ratio of two integers. I don't know what you get from the ratio of two real numbers.

      Comment

      • Zacariaz
        New Member
        • Jun 2015
        • 9

        #4
        it should also be noted that those =-a statements could potentially, though not likely, cause trouble. =(-a) would probably better, or at the very least some spacing.

        Comment

        Working...